<@pp.dropOutputFile/>
<#include "../common/util-easyui.ftl">
<#include "../common/validation.ftl">

<#-- generate package name -->

<#-- iterate table list-->
<#list doc.tables as table>
<#assign ID="id">
<#if table.idFieldName??>
<#assign ID=table.idFieldName>
</#if>
<#assign module = doc.artifactid?replace(".", "/") + "/" + table.moduleid?cap_first>
<#assign fileName = table.name?cap_first>
<#assign data_url = module + "/" + fileName>
<@pp.changeOutputFile name="/main/webapp/" + data_url + "/showitems_js.ftl"/>
<#noparse>
var ADD_CHECKER = null;
var EDT_CHECKER = null;
var $dataTable = null;
$(function(){
	$dataTable = $("#dataTable");
	$dataTable.edatagrid({
		url:"${base}/</#noparse>${data_url}<#noparse>/getPage.action",
		saveUrl: "${base}/</#noparse>${data_url}<#noparse>/create.action",
		updateUrl: "${base}/</#noparse>${data_url}<#noparse>/update.action",
		destroyUrl: "${base}/</#noparse>${data_url}/deleteItems.action",
		fit: true,
		nowrap: true,
		striped: true,
		sortName: "${ID}",
		sortOrder: "desc",
		remoteSort: false,
		idField:"${ID}",
		singleSelect:true,
		checkOnSelect:false,
		selectOnCheck:false,
		pageSize : 20,
		pageList : [20, 50, 100],
		frozenColumns:[[
			{field:"ck",checkbox:true},
			{title:"ID",hidden:true, field:"${ID}",width:40,sortable:true}
		]],
		columns:[[
			<#list table.fields as field>
			<#if field.name?lower_case != "id">
			{field:"obj.${field.name}", sortable : "true", title:"${field.label}", editor : ${getEditorValidator(field)}}<#if field_has_next>,</#if>
			</#if>
			</#list>
			<#noparse>
		]],
		pagination : true,
		rownumbers : true,
		toolbar : "#toolbar",
		onLoadError:function(){
		},
		onLoadSuccess : function(data){
			//$(".button-edit").linkbutton({plain:true}).tooltip();
		},
		onSuccess: function(index, data){
			showTip(data.success == 0, data.message);
		},
		onError: function(index, data){
			showTip(data.success == 0, data.message);
		}
	});
	jQuery.unserialize = f1;
	jQuery.fn.unserialize = f2;
	
	// Validators
	var options = {
		rules : </#noparse>${getRules(table)},
		message : ${getMessages(table)}<#noparse>,
		errorElement: "em",
		errorPlacement: function(error, element ) {
			// Add the `help-block` class to the error element
			error.addClass( "help-block" );
			error.insertAfter(element.parent("div"));
		},
		highlight: function ( element, errorClass, validClass ) {
			$(element).parents(".form-group").addClass("has-error").removeClass("has-success");
		},
		unhighlight: function (element, errorClass, validClass) {
			$(element).parents(".form-group").addClass("has-success").removeClass("has-error");
		}
	};
	ADD_CHECKER = $("#addForm").validate(options);
	EDT_CHECKER = $("#editForm").validate(options);
});

function deleteItems(){
	var rows = $("#dataTable").edatagrid("getChecked");
	if(rows.length > 0){
		$.messager.confirm("操作提示", "确定要删除记录？", function(r){
			if (r){
				var items = {};
				for(var i=0;i<rows.length;i++){
					items["items[" + i + "]"] = rows[i]["</#noparse>${ID}"];
				}
				$.post("<#noparse>${base}/</#noparse>${data_url}<#noparse>/deleteItems.action", 
					items, 
					function(data){
						if(data.success == 0){
							$("#dataTable").edatagrid("reload");
						}
						showTip(data.success == 0, data.message);				
					},
					"json"
				);
			}else{
			}
		});
	}
}

function search(){
	$dataTable.edatagrid('load', array2map($("#search_form").serializeArray()));
}

</#noparse>
</#list>